package com.coscon.controller;

import com.coscon.controller.base.BaseController;
import com.coscon.model.ResponseResult;
import com.coscon.model.api.ResponseCode;
import com.coscon.service.ConfigService;
import com.coscon.service.CoreAuthService;
import com.coscon.service.FileService;
import com.coscon.service.FtpService;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * 系统配置
 */
@Controller
@RequestMapping("/jm/manager/config")
public class ConfigController extends BaseController {

    @Resource
    private ConfigService configService;

    @Resource
    private CoreAuthService coreAuthService;
    @Resource
    private FileService fileService;
    @Resource
    private FtpService ftpService;

    @GetMapping("/index")
    public ModelAndView welcomePage(HttpServletRequest request){
        ModelAndView v = new ModelAndView("appPage/config");
        //默认用户名
        String defaultUserName = SystemUtils.USER_NAME;
        v.addObject("default_user_name",defaultUserName);
        //ftp默认目录
        String defaultFtpDir = this.ftpService.getDefaultFtpDir();
        v.addObject("default_ftp_dir",defaultFtpDir);
        //文件浏览的默认主目录
        String defaultUserHomePath = this.fileService.getUserHomePath();
        v.addObject("default_userHomePath",defaultUserHomePath);

        //获取安全相关配置的集合
        Map<String,String> securityConfigMap = this.configService.getSysconfigValRightLikeByKey("security_");
        v.addAllObjects(securityConfigMap);
        //日志相关的配置
        Map<String,String> logConfigMap = this.configService.getSysconfigValRightLikeByKey("syslog_");
        v.addAllObjects(logConfigMap);
        //文件浏览相关的配置
        Map<String,String> fileConfigMap = this.configService.getSysconfigValRightLikeByKey("file_");
        v.addAllObjects(fileConfigMap);

        return v;
    }


    /**
     * 保存安全配置
     * @param request
     * @return
     */
    @PostMapping("/saveSecurity")
    public @ResponseBody
    ResponseResult<Boolean> saveSecurity(
            HttpServletRequest request
    ){
        boolean f = configService.saveSecurityConfig(request.getParameterMap());
        if(f){//保存安全配置之后，重新加载shiro规则
            coreAuthService.reloadApacheShiroAuthFilterChains();
        }
        return new ResponseResult(ResponseCode.OK.getCode(),ResponseCode.OK.getMsg(),f);
    }


    @PostMapping("/saveLogConfig")
    public @ResponseBody
    ResponseResult<Boolean> saveLogConfig(
            HttpServletRequest request
    ){
        boolean f = configService.saveLogConfig(request.getParameterMap());
        return new ResponseResult(ResponseCode.OK.getCode(),ResponseCode.OK.getMsg(),f);
    }



    @PostMapping("/saveFileConfig")
    public @ResponseBody
    ResponseResult<Boolean> saveFileConfig(
            HttpServletRequest request
    ){
        boolean f = configService.saveFileConfig(request.getParameterMap());
        return new ResponseResult(ResponseCode.OK.getCode(),ResponseCode.OK.getMsg(),f);
    }



}
